/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cq.bd.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cq.base.domain.Page;
import com.cq.bd.dao.SDepartDao;
import com.cq.bd.dao.SRegionDao;
import com.cq.bd.domain.SDepart;
import com.cq.bd.domain.SRegion;
import com.cq.bd.service.RegionService;

/**
 * 组织部门管理服务
 * 
 * @author huangmy
 * @date   2009-10-5
 */
public class RegionServiceImpl implements RegionService {
    private static Log log = LogFactory.getLog(RegionServiceImpl.class);
    private SDepartDao sDepartDao;
    private SRegionDao sRegionDao;
    
    public Page pageRegions(Map<String, String> filter, Map<String, String> like, Map<String, String> sort, int pageNo,
            int pageSize) {        
        return this.sRegionDao.pagedQuery(filter, like, sort, pageNo, pageSize);
    }
    
    public Page pageDeparts(Map<String, String> filter, Map<String, String> like, Map<String, String> sort, int pageNo,
            int pageSize) {
        return this.sDepartDao.pagedQuery(filter, like, sort, pageNo, pageSize);
    }
    
    @SuppressWarnings("unchecked")
    public List<SDepart> doGetDepartsByRegionId(Long regionId) {
        return this.sDepartDao.find("from SDepart where regionId=? and state=1", regionId);
    }
    
    public void doDeleteDepart(SDepart depart) {
        this.sDepartDao.remove(depart);
    }
    
    public int checkRegionCode(String regionCode) {
        if (log.isDebugEnabled()) {
            log.debug("regionCode: " + regionCode);
        }
        List<SRegion> regions = this.sRegionDao.findBy("regionCode", regionCode);
        if (regions == null || regions.size() <= 0) {
            return 1;
        }
        return 0;
    }
    
    public int checkDepartCode(String departCode) {
        if (log.isDebugEnabled()) {
            log.debug("departCode: " + departCode);
        }
        List<SDepart> regions = this.sDepartDao.findBy("departCode", departCode);
        if (regions == null || regions.size() <= 0) {
            return 1;
        }
        return 0;
    }
    
    public void doCreateDepart(SDepart depart) {
        this.sDepartDao.create(depart);
    }

    public void doCreateRegion(SRegion region) {
        this.sRegionDao.create(region);
    }

    public SDepart doGetDepart(Long departId) {   
        if (departId == null) {
            return null;
        }
        return this.sDepartDao.get(departId);
    }

    public List<SDepart> doGetDeparts() {
        return this.sDepartDao.list();
    }
    
    @SuppressWarnings("unchecked")
    public List<SDepart> doGetValidDeparts() {
        return this.sDepartDao.find("from SDepart where state=1");
    }

    public SRegion doGetRegion(Long regionId) { 
        if (regionId == null) {
            return null;
        }
        return this.sRegionDao.get(regionId);
    }

    @SuppressWarnings("unchecked")
    public List<SRegion> doGetRegions(Long upperRegionId) {
        return this.sRegionDao.find("from SRegion where upperRegionId=?", upperRegionId);
    }
    
    @SuppressWarnings("unchecked")
    public List<SRegion> doGetAllRegions() {
        return this.sRegionDao.find("from SRegion where state=1");
    }
    
    public void doDeleteRegion(SRegion region) {
        this.sRegionDao.remove(region);
    }

    public void doUpdateDepart(SDepart depart) {
        this.sDepartDao.update(depart);
    }

    public void doUpdateRegion(SRegion region) {
        this.sRegionDao.update(region);
    }

    public void setSDepartDao(SDepartDao departDao) {
        sDepartDao = departDao;
    }

    public void setSRegionDao(SRegionDao regionDao) {
        sRegionDao = regionDao;
    }

}
